package com.yunbiao.ybbusstopplate.ui.fragment

import android.os.Handler
import android.os.Looper
import android.text.TextUtils
import android.util.Log
import android.view.View
import com.blankj.utilcode.util.PhoneUtils
import com.blankj.utilcode.util.ToastUtils
import com.yunbiao.ybbusstopplate.R
import com.yunbiao.ybbusstopplate.base.BaseFragment
import com.yunbiao.ybbusstopplate.base.ContentValue
import com.yunbiao.ybbusstopplate.utils.ProgressDialogUtils
import com.yunbiao.ybbusstopplate.utils.SpTool
import com.yunbiao.ybbusstopplate.utils.eventbus.EventMsgTool
import kotlinx.android.synthetic.main.dialog_activate.*

class VerifyFragment :BaseFragment(){

    companion object{
        fun getInstance():VerifyFragment{
            val verifyFragment = VerifyFragment()
            return verifyFragment
        }
    }

    override fun getViewId(): Int {
        return R.layout.fragment_verify
    }

    override fun initData() {
        if (ContentValue.autoVerify){
            val serial = PhoneUtils.getSerial()
            System.out.println("serial-->"+serial)
            edt_device_number.setText(PhoneUtils.getSerial())
        }

        btn_confirm.setOnClickListener({
            val inputSerNum = edt_device_number.text.toString().trim()
            if (TextUtils.isEmpty(inputSerNum)){
                ToastUtils.showShort("激活码不能为空")
                return@setOnClickListener
            }
            ProgressDialogUtils.showProgressDialog(requireContext(),"激活校验中，请稍等...")
            Handler(Looper.getMainLooper()).postDelayed(object :Runnable{
                override fun run() {
                    cheakPsd(inputSerNum)
                }
            },500)
        })

        img_clean.setOnClickListener({
            edt_device_number.setText("")
        })
    }

    override fun initListener() {

    }

    private fun cheakPsd(inputSerNum:String){
        val serial = PhoneUtils.getSerial()
        System.out.println("serial----->"+serial)

        Log.d("check", "check app code inputSerNum: " + inputSerNum)

        Log.d("check", "check app code serial: " + serial)

        ProgressDialogUtils.dismissProgressDialog()
        if (inputSerNum.equals(serial)){
            tv_tip_error.visibility= View.INVISIBLE
            ToastUtils.showShort("校验成功")
            //储存数据
            SpTool.saveAppSerialNumber(inputSerNum)
            //显示主逻辑页面
            EventMsgTool.getInstance().postGoMianFragment()
        }else{
            tv_tip_error.visibility= View.VISIBLE
            ToastUtils.showShort("校验失败，请重试")
        }
    }

    override fun onDestroy() {
        super.onDestroy()
        ProgressDialogUtils.dismissProgressDialog()
    }
}